#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"DynamicArray.h"

struct Student
{
    char *name;
    int age;
};
typedef struct Student Stu;

Stu *CreateStu(const char* name,int age)
{
    Stu *s = (Stu *)malloc(sizeof(Stu));
    if(s==NULL)
    {
        printf("学生申请空间失败\n");
        return NULL;
    }
    s->name = strdup(name);
    s->age = age;
    return s;
}
void PrintStu(ElementType ptr)
{
    Stu *s = (Stu *)ptr;
    printf("学生姓名：%s 年龄：%d\n",s->name,s->age);
}
void FreeStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    free(s->name);
    free(s);
}
bool IsNameEqual(void *ptr1,void *ptr2)
{
    Stu *s = (Stu *)ptr1;
    char *name = (char *)ptr2;
    return strcmp(s->name, name)==0;
}
void PrintStuArray(void* ptr)
{
    printf("班级=====================\n");
    DArray *a = (DArray *)ptr;
    PrintArray(a, PrintStu);
}
typedef struct
{
    char *name;
    DArray *stuArray;

}Class;
Class* CreateClass(const char* name)
{
    Class *c = (Class *)malloc(sizeof(Class));
    if(c==NULL)
    {
        printf("class malloc error!\n");
        return NULL;
    }
    c->name = strdup(name);
    c->stuArray = InitDArray(10, FreeStu);
    return c;
}
void FreeClass(void *ptr)
{
    Class *c = (Class *)ptr;
    free(c->name);
    FreeDArray(c->stuArray);
}
bool IsClssNameEql(void *ptr1,void* ptr2)
{
    Class *c = (Class *)ptr1;
    char *name = (char *)ptr2;
    return strcmp(c->name, name) == 0;
}
void InsrtStu2Clss(DArray *a,char *ClassName,Stu *s)
{
    Class*c=(Class*)FindByElement(a, ClassName, IsClssNameEql);
    if(c==NULL)
    {
        printf("没找到班级.\n");
        return;
    }
    InsertTail(c->stuArray, s);
}
void PrintClass(void* ptr)
{
    Class *c = (Class *)ptr;
    printf("班级名称：%s\n", c->name);
    PrintArray(c->stuArray, PrintStu);
}
bool SortByAge(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    return s1->age > s2->age;
}
bool SortByName(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    return strcmp(s1->name,s2->name)>0;
}
int SearchByName(void *ptr1,void*ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    char *name = (char *)ptr2;
    return strcmp(s1->name, name);
}
int main()
{

    
    // DArray *a = InitDArray(3,FreeDArray);
    // DArray *class267 = InitDArray(10, FreeStu);
    // InsertTail(class267, CreateStu("张三", 20));
    // InsertTail(class267, CreateStu("李四", 22));
    // InsertTail(class267, CreateStu("王五", 24));
    // DArray *class262 = InitDArray(10, FreeStu);
    // InsertTail(class262, CreateStu("赵六", 20));
    // InsertTail(class262, CreateStu("牛牛", 22));
    // InsertTail(class262, CreateStu("三三", 24));
    // InsertTail(a, class262);
    // InsertTail(a, class267);
    // PrintArray(a, PrintStuArray);
    
    // PrintArray(a, PrintStu);
    // Stu *s=(Stu*)FindByElement(a, "李四", IsNameEqual);
    // if(s==NULL)
    // {
    //     printf("没找到！\n");
    // }
    // else{
    //     RemoveByValue(a, s);
    //     PrintArray(a, PrintStu);
    // }
    // FreeDArray(a);
    DArray *a = InitDArray(10, FreeStu);
    InsertTail(a, CreateStu("张三", 20));
    InsertTail(a, CreateStu("李四", 19));
    InsertTail(a, CreateStu("王五", 22));

    DArrayFastSort(a,SortByName);
    PrintArray(a, PrintStu);
    ElementType ptr=BinarySearch(a, "张三",SearchByName);
    if(ptr==NULL)
    {
        printf("没找到\n");

    }
    else
    {
        PrintStu(ptr);
    }
    FreeDArray(a);
    return 0;
}